package com.moli.iov.admin.web.controller;

import com.google.common.collect.Maps;
import com.moli.iov.admin.model.vo.req.*;
import com.moli.iov.down.enums.OperateTypeEnum;
import com.moli.iov.notify.model.dto.*;
import com.moli.iov.notify.remote.INoticeRemoteService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.OperationTypeEnum;
import com.moli.iov.sys.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * Vehicle control and alarm notice business controller.
 *
 * @author OAK
 * @version 3.0
 * @since 2019/10/15 14:27 PM.
 *
 */
@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController extends BaseController {

    @Reference(group = "${service.group}", check = false)
    INoticeRemoteService noticeRemoteService;

    /**
     * Get all vehicle control command.
     * @return All vehicle control command.
     */
    @GetMapping("/alarm/vehicleCtl/commands")
    public RestResponse<List<Map<String, Object>>> queryAllVehicleCtlCommands(){
        List<Map<String, Object>> operateTypeList = OperateTypeEnum.getCtlVehicleEnums().stream().map(operateTypeEnum ->
        {
            Map<String, Object> operateType = new HashMap(2) {
                {
                    put("dictCode", operateTypeEnum.getFlag());
                    put("dictValue", operateTypeEnum.getMsg());
                }
            };
            return operateType;
        }).collect(Collectors.toList());
        return RestResponse.success(operateTypeList);
    }

    /**
     * Query vehicle control notice collection.
     * @param noticeAlarmVo Alarm notice value object.
     * @return Whether success.
     */
    @PostMapping("/alarm/list")
    public RestResponse<List<NoticeAlarmDto>> queryAllNoticeAlarmByProperties(@RequestBody NoticeAlarmVo noticeAlarmVo){
        assembleUserInfo(noticeAlarmVo);
        NoticeAlarmDto noticeAlarmDto = new NoticeAlarmDto();
        noticeAlarmDto.setAlarmLevelId(noticeAlarmVo.getAlarmLevelId());
        noticeAlarmDto.setCompanyId(noticeAlarmVo.getCompanyId());
        if(UserTypeEnum.COMPANY.getFlag() == noticeAlarmVo.getSysUserType()){
            noticeAlarmDto.setCompanyId(noticeAlarmVo.getSysCompanyId());
        }
        return noticeRemoteService.queryAllNoticeAlarmByProperties(noticeAlarmDto);
    }

    /**
     * Query alarm notice collection.
     * @param noticeVehicleCtlVo Vehicle control notice value object.
     * @return Whether success.
     */
    @PostMapping("/vehicleCtl/list")
    public RestResponse<List<NoticeVehicleCtlDto>> queryAllNoticeVehicleCtlByProperties(@RequestBody NoticeVehicleCtlVo  noticeVehicleCtlVo){
        assembleUserInfo(noticeVehicleCtlVo);
        NoticeVehicleCtlDto noticeVehicleCtlDto = new NoticeVehicleCtlDto();
        noticeVehicleCtlDto.setCompanyId(noticeVehicleCtlVo.getCompanyId());
        noticeVehicleCtlDto.setVehicleCtlCode(noticeVehicleCtlVo.getVehicleCtlCode());
        if(UserTypeEnum.COMPANY.getFlag() == noticeVehicleCtlVo.getSysUserType()){
            noticeVehicleCtlDto.setCompanyId(noticeVehicleCtlVo.getSysCompanyId());
        }
        return noticeRemoteService.queryAllNoticeVehicleCtlByProperties(noticeVehicleCtlDto);
    }

    /**
     * According Sms notice id to query sms contact.
     * @param noticeSmsVo Sms notice value object.
     * @return Whether success.
     */
    @PostMapping("/vehicleCtl/sms/list")
    public RestResponse<String> queryVehicleCtlMobileByNoticeSms(@RequestBody @Validated NoticeSmsVo noticeSmsVo){
        assembleUserInfo(noticeSmsVo);
        Long companyId = noticeSmsVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeSmsVo.getSysUserType()){
            companyId = noticeSmsVo.getSysCompanyId();
        }
        return noticeRemoteService.queryMobileByNoticeSms(companyId,
                noticeSmsVo.getSmsType(), noticeSmsVo.getSmsCode());
    }

    /**
     * According Mail notice id to query mail contact.
     * @param noticeMailVo Mail notice value object.
     * @return Whether success.
     */
    @PostMapping("/vehicleCtl/mail/list")
    public RestResponse<String> queryVehicleCtlMailByNoticeMail(@RequestBody @Validated NoticeMailVo noticeMailVo){
        assembleUserInfo(noticeMailVo);
        Long companyId = noticeMailVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeMailVo.getSysUserType()){
            companyId = noticeMailVo.getSysCompanyId();
        }
        return noticeRemoteService.queryMailByNoticeMail(companyId,
                noticeMailVo.getMailType(), noticeMailVo.getMailCode());
    }

    /**
     * According Sms notice id to query sms contact.
     * @param noticeSmsVo Sms notice value object.
     * @return Whether success.
     */
    @PostMapping("/alarm/sms/list")
    public RestResponse<String> queryMobileByNoticeSms(@RequestBody @Validated NoticeSmsVo noticeSmsVo){
        assembleUserInfo(noticeSmsVo);
        Long companyId = noticeSmsVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeSmsVo.getSysUserType()){
            companyId = noticeSmsVo.getSysCompanyId();
        }
        return noticeRemoteService.queryMobileByNoticeSms(companyId,
                noticeSmsVo.getSmsType(), noticeSmsVo.getSmsCode());
    }

    /**
     * According Mail notice id to query mail contact.
     * @param noticeMailVo Mail notice value object.
     * @return Whether success.
     */
    @PostMapping("/alarm/mail/list")
    public RestResponse<String> queryMailByNoticeMail(@RequestBody @Validated NoticeMailVo noticeMailVo){
        assembleUserInfo(noticeMailVo);
        Long companyId = noticeMailVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeMailVo.getSysUserType()){
            companyId = noticeMailVo.getSysCompanyId();
        }
        return noticeRemoteService.queryMailByNoticeMail(companyId,
                noticeMailVo.getMailType(), noticeMailVo.getMailCode());
    }

    /**
     * According notice sms id and mobile to add contact.
     * @param noticeSmsVo Sms notice value object.
     * @return Whether success.
     */
    @PostMapping("/vehicleCtl/sms/add")
    public RestResponse<String> addVehicleCtlNoticeSms(@RequestBody @Validated NoticeSmsVo noticeSmsVo){
        assembleUserInfo(noticeSmsVo);
        NoticeSmsDto noticeSmsDto = new NoticeSmsDto();
        Long companyId = noticeSmsVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeSmsVo.getSysUserType()){
            companyId = noticeSmsVo.getSysCompanyId();
        }
        noticeSmsDto.setCompanyId(companyId);
        noticeSmsDto.setSmsCode(noticeSmsVo.getSmsCode());
        noticeSmsDto.setSmsType(noticeSmsVo.getSmsType());
        noticeSmsDto.setMobile(noticeSmsVo.getMobile());
        return noticeRemoteService.addNoticeSms(noticeSmsDto);
    }

    /**
     * According notice mail id and mail to add contact.
     * @param noticeMailVo Mail notice value object.
     * @return Whether success.
     */
    @PostMapping("/vehicleCtl/mail/add")
    public RestResponse<String> addVehicleCtlNoticeMail(@RequestBody @Validated NoticeMailVo noticeMailVo){
        assembleUserInfo(noticeMailVo);
        NoticeMailDto noticeMailDto = new NoticeMailDto();
        Long companyId = noticeMailVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeMailVo.getSysUserType()){
            companyId = noticeMailVo.getSysCompanyId();
        }
        noticeMailDto.setCompanyId(companyId);
        noticeMailDto.setMailCode(noticeMailVo.getMailCode());
        noticeMailDto.setMailType(noticeMailVo.getMailType());
        noticeMailDto.setMail(noticeMailVo.getMail());
        return noticeRemoteService.addNoticeMail(noticeMailDto);
    }

    /**
     * According notice sms id and mobile to add contact.
     * @param noticeSmsVo Sms notice value object.
     * @return Whether success.
     */
    @PostMapping("/alarm/sms/add")
    public RestResponse<String> addNoticeSms(@RequestBody @Validated NoticeSmsVo noticeSmsVo){
        assembleUserInfo(noticeSmsVo);
        NoticeSmsDto noticeSmsDto = new NoticeSmsDto();
        Long companyId = noticeSmsVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeSmsVo.getSysUserType()){
            companyId = noticeSmsVo.getSysCompanyId();
        }
        noticeSmsDto.setCompanyId(companyId);
        noticeSmsDto.setSmsCode(noticeSmsVo.getSmsCode());
        noticeSmsDto.setSmsType(noticeSmsVo.getSmsType());
        noticeSmsDto.setMobile(noticeSmsVo.getMobile());
        return noticeRemoteService.addNoticeSms(noticeSmsDto);
    }

    /**
     * According notice mail id and mail to add contact.
     * @param noticeMailVo Mail notice value object.
     * @return Whether success.
     */
    @PostMapping("/alarm/mail/add")
    public RestResponse<String> addNoticeMail(@RequestBody @Validated NoticeMailVo noticeMailVo){
        log.info("修改通知提醒电子邮件，{}",noticeMailVo);
        assembleUserInfo(noticeMailVo);
        NoticeMailDto noticeMailDto = new NoticeMailDto();
        Long companyId = noticeMailVo.getCompanyId();
        if(UserTypeEnum.COMPANY.getFlag() == noticeMailVo.getSysUserType()){
            companyId = noticeMailVo.getSysCompanyId();
        }
        noticeMailDto.setCompanyId(companyId);
        noticeMailDto.setMailCode(noticeMailVo.getMailCode());
        noticeMailDto.setMailType(noticeMailVo.getMailType());
        noticeMailDto.setMail(noticeMailVo.getMail());
        return noticeRemoteService.addNoticeMail(noticeMailDto);
    }

//    /**
//     * According alarm notice id enabled all or sms or email.
//     * @param noticeMotionVo Vehicle control or alarm notice value object.
//     * @return Whether success.
//     */
//    @PutMapping("/alarm/sms")
//    public RestResponse<String> noticeAlarmSms(@RequestBody @Validated NoticeMotionVo noticeMotionVo){
//        Integer id = noticeMotionVo.getId();
//        String motion = noticeMotionVo.getMotion();
//        return noticeRemoteService.enableNoticeAlarm(id, motion);
//    }

    /**
     * According alarm notice id enabled all or sms or email.
     * @param alarmAndVehicleCtlNoticeMotionVo Vehicle control or alarm notice value object.
     * @return Whether success.
     */
    @Deprecated
    @PutMapping("/alarm/enabled")
    public RestResponse<String> enabledNoticeAlarm(@RequestBody @Validated AlarmAndVehicleCtlNoticeMotionVo alarmAndVehicleCtlNoticeMotionVo){
        Integer id = alarmAndVehicleCtlNoticeMotionVo.getId();
        String motion = alarmAndVehicleCtlNoticeMotionVo.getMotion();
        return noticeRemoteService.enableNoticeAlarm(id, motion);
    }

    /**
     * According alarm notice id disabled all or sms or email.
     * @param alarmAndVehicleCtlNoticeMotionVo Vehicle control or alarm notice value object.
     * @return Whether success.
     */
    @Deprecated
    @PutMapping("/alarm/disabled")
    public RestResponse<String> disabledNoticeAlarm(@RequestBody @Validated AlarmAndVehicleCtlNoticeMotionVo alarmAndVehicleCtlNoticeMotionVo){
        Integer id = alarmAndVehicleCtlNoticeMotionVo.getId();
        String motion = alarmAndVehicleCtlNoticeMotionVo.getMotion();
        return noticeRemoteService.disabledNoticeAlarm(id, motion);
    }

    /**
     * According vehicle control notice id disabled all or sms or email.
     * @param alarmAndVehicleCtlNoticeMotionVo Vehicle control or alarm notice value object.
     * @return Whether success.
     */
    @Deprecated
    @PutMapping("/vehicleCtl/enabled")
    public RestResponse<String> enabledNoticeVehicleCtl(@RequestBody @Validated AlarmAndVehicleCtlNoticeMotionVo alarmAndVehicleCtlNoticeMotionVo){
        Integer id = alarmAndVehicleCtlNoticeMotionVo.getId();
        String motion = alarmAndVehicleCtlNoticeMotionVo.getMotion();
        return noticeRemoteService.enabledNoticeVehicleCtl(id, motion);
    }

    /**
     * According vehicle control notice id disabled all or sms or email.
     * @param alarmAndVehicleCtlNoticeMotionVo Vehicle control or alarm notice value object.
     * @return Whether success.
     */
    @Deprecated
    @PutMapping("/vehicleCtl/disabled")
    public RestResponse<String> disabledNoticeVehicleCtl(@RequestBody @Validated AlarmAndVehicleCtlNoticeMotionVo alarmAndVehicleCtlNoticeMotionVo){
        Integer id = alarmAndVehicleCtlNoticeMotionVo.getId();
        String motion = alarmAndVehicleCtlNoticeMotionVo.getMotion();
        return noticeRemoteService.disabledNoticeVehicleCtl(id, motion);
    }

}